package org.boticelli.tag;

import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Formatter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.boticelli.controller.DecoratedLog;
import org.boticelli.model.LogType;
import org.boticelli.util.DateUtil;
import org.springframework.util.Assert;

/**
 * Renders the log output.
 * @author shelmberger
 */
public class LogRenderTag
        extends TagSupport
{
    private static final long serialVersionUID = -8802222533528745380L;

    private boolean dateHeader;

    private List<DecoratedLog> logs;

    public void setDateHeader(boolean dateHeader)
    {
        this.dateHeader = dateHeader;
    }

    public void setLogs(List<DecoratedLog> logs)
    {
        this.logs = logs;
    }

    @Override
    public int doStartTag() throws JspException
    {
        try
        {
            Assert.notNull(logs, "No logs.");

            JspWriter out = pageContext.getOut();
            if (logs.size() == 0)
            {
                out.print("No results found.");
            }
            else
            {
                SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MMM yyyy");

                String contextPath = ((HttpServletRequest)pageContext.getRequest()).getContextPath();

                Formatter formatter=new Formatter(out);

                String lastTime = "";
                Integer lastDay = null;
                for (DecoratedLog log : logs)
                {
                    if (dateHeader)
                    {
                        Integer day = log.getDay();
                        if (lastDay == null || !lastDay.equals(day))
                        {
                            String date = dateFormat.format(log.getMoment());
                            out.print("<div class=\"clearIE\">" +
                            		"<span class=\"time\">&nbsp;</span>" +
                            		"<span class=\"nick\">&nbsp;</span>" +
                            		"<div class=\"dateHeader alt\"><a href=\""+contextPath+"/app/log?day="+DateUtil.toYYYYMMDD(log.getMoment())+"\">" + date + "</a></div><span class=\"score\">&nbsp;</span></div>\n");
                        }
                        lastDay = day;
                    }
                    String time = timeFormat.format(log.getMoment());
                    out.print("<div class=\"clearIE "+log.getColorClass()+"\">");
                    out.print("<span class=\"time\">");
                    if (!lastTime.equals(time))
                    {
                        out.print(time);
                    }
                    else
                    {
                        out.print("&nbsp;");
                    }
                    lastTime=time;
                    out.print("</span>");

                    out.print("<span class=\"nick\" title=\""+log.getIdent()+"\">");
                    switch(log.getType())
                    {
                        case ACTION:
                            out.print("*");
                            break;
                        case INFO:
                            out.print("-!-");
                            break;
                        case PRIVMSG:

                            String nick = log.getNick();
                            if (nick.length() > 22)
                            {
                                nick=nick.substring(0,22)+"..";
                            }
                            out.print("&lt;"+nick+"&gt;");
                            break;
                    }
                    out.print("</span><span class=\"message "+log.getColorClass()+"\">");
                    if (log.getType() != LogType.PRIVMSG)
                    {
                        out.print(log.getNick()+" ");
                    }
                    out.print(log.getText()+"</span>");
                    if (dateHeader)
                    {
                        out.print("<span class=\"score\">Score ");
                        formatter.format("%.2f", log.getScore());
                        out.print("</span>");
                    }

                    out.print("</div>\n");
                }
            }
            return SKIP_BODY;
        }
        catch (IOException e)
        {
            throw new JspException(e);
        }
    }
}
